package com.okcl.manage.service.impl;

import com.okcl.common.utils.DateUtils;
import com.okcl.manage.domain.Book;
import com.okcl.manage.domain.BorrowRecord;
import com.okcl.manage.domain.vo.BorrowRecordVo;
import com.okcl.manage.mapper.BookMapper;
import com.okcl.manage.mapper.BorrowRecordMapper;
import com.okcl.manage.mapper.UserInfoMapper;
import com.okcl.manage.service.IBorrowRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 借阅记录Service业务层处理
 *
 * @author Mike
 * @date 2025-05-15
 */
@Service
public class BorrowRecordServiceImpl implements IBorrowRecordService {
    @Autowired
    private BorrowRecordMapper borrowRecordMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private BookMapper bookMapper;

    /**
     * 查询借阅记录
     *
     * @param id 借阅记录主键
     * @return 借阅记录
     */
    @Override
    public BorrowRecord selectBorrowRecordById(Long id) {
        return borrowRecordMapper.selectBorrowRecordById(id);
    }

    /**
     * 查询借阅记录列表
     *
     * @param borrowRecord 借阅记录
     * @return 借阅记录
     */
    @Override
    public List<BorrowRecord> selectBorrowRecordList(BorrowRecord borrowRecord) {
        return borrowRecordMapper.selectBorrowRecordList(borrowRecord);
    }

    /**
     * 新增借阅记录
     *
     * @param borrowRecord 借阅记录
     * @return 结果
     */
    @Override
    public int insertBorrowRecord(BorrowRecord borrowRecord) {
        borrowRecord.setCreateTime(DateUtils.getNowDate());
        return borrowRecordMapper.insertBorrowRecord(borrowRecord);
    }

    /**
     * 修改借阅记录
     *
     * @param borrowRecord 借阅记录
     * @return 结果
     */
    @Override
    public int updateBorrowRecord(BorrowRecord borrowRecord) {
        borrowRecord.setUpdateTime(DateUtils.getNowDate());
        return borrowRecordMapper.updateBorrowRecord(borrowRecord);
    }

    /**
     * 批量删除借阅记录
     *
     * @param ids 需要删除的借阅记录主键
     * @return 结果
     */
    @Override
    public int deleteBorrowRecordByIds(Long[] ids) {
        return borrowRecordMapper.deleteBorrowRecordByIds(ids);
    }

    /**
     * 删除借阅记录信息
     *
     * @param id 借阅记录主键
     * @return 结果
     */
    @Override
    public int deleteBorrowRecordById(Long id) {
        return borrowRecordMapper.deleteBorrowRecordById(id);
    }

    /**
     * 查询借阅记录列表(包含对应的用户名和图书名)
     *
     * @param borrowRecord
     * @return
     */
    @Override
    public List<BorrowRecordVo> selectBorrowRecordVoList(BorrowRecord borrowRecord) {
        List<BorrowRecord> borrowRecords = borrowRecordMapper.selectBorrowRecordList(borrowRecord);
        List<BorrowRecordVo> collect = borrowRecords.stream().map(item -> {
            //创建一个vo对象
            BorrowRecordVo borrowRecordVo = new BorrowRecordVo();
            borrowRecordVo.setUserName(userInfoMapper.getUserNameById(item.getUserId()));
            // 获取书籍信息并设置书名
            Book book = bookMapper.selectBookById(item.getBookId());
            if (book != null) {
                borrowRecordVo.setBookName(book.getBookName());
            } else {
                borrowRecordVo.setBookName("未知书籍"); // 或者根据需求处理缺失书籍的情况
            }
            BeanUtils.copyProperties(item, borrowRecordVo);
            return borrowRecordVo;
        }).collect(Collectors.toList());
        return collect;
    }
}
